import type { Coupon } from '@/types/coupon';
import { CouponType } from '@/types/coupon';

/**
 * 获取优惠券类型文本
 * @param type CouponType
 * @returns string
 */
export const getCouponTypeText = (type: CouponType): string => {
  const typeMap: Record<CouponType, string> = {
    [CouponType.Discount]: '折扣券',
    [CouponType.Cash]: '现金券',
    [CouponType.Reduction]: '满减券',
    [CouponType.Duration]: '时长券',
    [CouponType.Free]: '免费券',
    [CouponType.Gift]: '赠品券',
    [CouponType.Special]: '特价券',
  };
  return typeMap[type] || '未知类型';
};

/**
 * 获取优惠券类型对应的颜色
 * @param type CouponType
 * @returns 'success' | 'primary' | 'warning' | 'error' | 'info'
 */
export const getCouponTypeColor = (type: CouponType): 'success' | 'primary' | 'warning' | 'error' | 'info' => {
  const colorMap: Record<CouponType, 'success' | 'primary' | 'warning' | 'error' | 'info'> = {
    [CouponType.Discount]: 'primary',
    [CouponType.Cash]: 'success',
    [CouponType.Reduction]: 'warning',
    [CouponType.Duration]: 'info',
    [CouponType.Free]: 'error',
    [CouponType.Gift]: 'primary',
    [CouponType.Special]: 'warning',
  };
  return colorMap[type] || 'info';
};

/**
 * 格式化优惠券面值
 * @param coupon Coupon
 * @returns string
 */
export const formatCouponValue = (coupon: Coupon): string => {
  if (!coupon) return '-';
  
  switch (coupon.type) {
    case CouponType.Discount:
      return `${(coupon.discount || 0) / 10}折`;
    case CouponType.Cash:
      return `¥${((coupon.value || 0) / 100).toFixed(2)}`;
    case CouponType.Reduction:
      return `满${((coupon.min_consume || 0) / 100).toFixed(2)}减${((coupon.value || 0) / 100).toFixed(2)}`;
    case CouponType.Duration:
      return `${coupon.duration || 0}分钟`;
    case CouponType.Free:
      return '免费';
    case CouponType.Gift:
      return '赠品';
    case CouponType.Special:
      return `¥${((coupon.value || 0) / 100).toFixed(2)}`;
    default:
      return '-';
  }
};

/**
 * 格式化使用门槛
 * @param coupon Coupon
 * @returns string
 */
export const formatMinConsume = (coupon: Coupon): string => {
  if (!coupon) return '无门槛';
  
  if (coupon.min_consume && coupon.min_consume > 0) {
    return `满${(coupon.min_consume / 100).toFixed(2)}元可用`;
  }
  return '无门槛';
};
